﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToolwebAQ.database;
using System.Data.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace ToolwebAQ.adminaq
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        aquariusDBDataContext db = new aquariusDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            BindGrid();
        }
        private void BindGrid()
        {

            var allmenu = from menu in db.Tbl_pagemenus
                          from cate in db.Tbl_categorymenus
                          where menu.idcatemenu == cate.idcatemenu
                          select new
                          {
                              idmenupage = menu.idmenupage,
                              Namepage = menu.Namepage,
                              Urlpage = menu.Urlpage,
                              Isused = menu.Isused,
                              Namemenu = cate.Namemenu
                          };
            SqlCommand cmd = (SqlCommand)db.GetCommand(allmenu);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            devg.DataSource = ds;
            devg.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strMultiIds;
            List<object> selectedRowList;

            strMultiIds = string.Empty;
            selectedRowList = (devg.GetSelectedFieldValues(new string[] { "ID" }));
            if (selectedRowList.Count == 0)
            {
                return;
            }

            for (int i = 0; i < selectedRowList.Count; i++)
            { strMultiIds += selectedRowList[i].ToString() + ","; }

            strMultiIds.TrimEnd(',');
            try
            {
                delete(strMultiIds.TrimEnd(','));
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
            }
            BindGrid();
        }

        private void delete(string p)
        {
            if (p.Equals(""))
                return;
            try
            {
                deleteRows(p, "ContactDetails");
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
            }
        }

        private void deleteRows(string selectedid, string tablename)
        {
            //StringBuilder sqlQuery = new StringBuilder();
            //selectedid = selectedid.TrimEnd(',') + ",0";
            //StringBuilder whereClause = new StringBuilder();
            //whereClause.Append(" WHERE ID IN (" + selectedid + ") ");
            //sqlQuery.Append(" DELETE FROM ");
            //sqlQuery.Append(tablename + whereClause.ToString());
            //try
            //{
            //    SqlConnection conn = new SqlConnection("Connection string here");
            //    conn.Open();
            //    SqlCommand cmd;

            //    cmd = new SqlCommand(sqlQuery.ToString());
            //    cmd.Connection = conn;
            //    DataSet dsClaimCat = new DataSet();

            //    SqlDataAdapter dapt = new SqlDataAdapter(cmd);
            //    dapt.Fill(dsClaimCat);
            //    devg.DataSource = dsClaimCat;
            //    devg.DataBind();
            //    conn.Close();
            //    lblMessage.Visible = true;
            //    lblMessage.Text = "Record(s) Deleted";
            //}
            //catch (Exception ex)
            //{
            //    lblMessage.Visible = true;
            //    lblMessage.Text = ex.Message;
            //}
        }

        protected void devg_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            BindGrid();
        }

        protected void devg_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            //StringBuilder sqlQuery = new StringBuilder();
            //SqlConnection conn = new SqlConnection("Connection string here");
            //sqlQuery.Append("UPDATE ContactDetails SET ");
            //sqlQuery.Append("varName = '" + e.NewValues["varName"].ToString() + "',");
            //sqlQuery.Append("varContactNo = '" + e.NewValues["varContactNo"].ToString() + "',");
            //sqlQuery.Append("varEmail = '" + e.NewValues["varEmail"].ToString() + "',");
            //sqlQuery.Append("varAddress = '" + e.NewValues["varAddress"].ToString() + "'");
            //sqlQuery.Append(" WHERE ID= " + e.Keys["ID"] + "");
            //conn.Open();
            //SqlCommand cmd;

            //cmd = new SqlCommand(sqlQuery.ToString());
            //cmd.Connection = conn;
            //DataSet dsClaimCat = new DataSet();

            //SqlDataAdapter dapt = new SqlDataAdapter(cmd);
            //int i = cmd.ExecuteNonQuery();
            //if (i > 0)
            //{
            //    e.Cancel = true;
            //    devg.CancelEdit();
            //}
            //lblMessage.Visible = true;
            //lblMessage.Text = "Record(s) Updated";
            //BindGrid();
            //conn.Close();
        }

        protected void devg_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            //StringBuilder sqlQuery = new StringBuilder();
            //SqlConnection conn = new SqlConnection("connection string here");
            //sqlQuery.Append("INSERT INTO ContactDetails(varName,varContactNo,varEmail,varAddress) VALUES ( ");
            //sqlQuery.Append("'" + e.NewValues["varName"].ToString() + "',");
            //sqlQuery.Append("'" + e.NewValues["varContactNo"].ToString() + "',");
            //sqlQuery.Append("'" + e.NewValues["varEmail"].ToString() + "',");
            //sqlQuery.Append("'" + e.NewValues["varAddress"].ToString() + "')");
            //conn.Open();
            //SqlCommand cmd;

            //cmd = new SqlCommand(sqlQuery.ToString());
            //cmd.Connection = conn;
            //DataSet dsClaimCat = new DataSet();

            //SqlDataAdapter dapt = new SqlDataAdapter(cmd);
            //int i = cmd.ExecuteNonQuery();
            //if (i > 0)
            //{
            //    e.Cancel = true;
            //    devg.CancelEdit();
            //}
            //lblMessage.Visible = true;
            //lblMessage.Text = "Record(s) Saved";
            //BindGrid();
            //conn.Close();
        }
    }
}